package com.zp.base.admin.service.impl;

import com.zp.base.admin.common.utils.PageUtils;
import com.zp.base.admin.common.utils.Query;
import com.zp.base.admin.dao.RoleMenuDao;
import com.zp.base.admin.entity.RoleMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zp.base.admin.dao.MenuDao;
import com.zp.base.admin.entity.MenuEntity;
import com.zp.base.admin.service.MenuService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 张鹏
 */
@Service("menuService")
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {

    @Autowired
    MenuDao menuDao;
    @Autowired
    RoleMenuDao roleMenuDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MenuEntity> page = this.page(
                new Query<MenuEntity>().getPage(params),
                new QueryWrapper<MenuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MenuEntity> getAllMenus() {
        //获取所有菜单
        List<MenuEntity> menuEntities = menuDao.selectList(null);
        //获取所有一级菜单
        List<MenuEntity> oneLevelMenus = menuEntities.stream()
                .filter(menuEntity -> {
                    return menuEntity.getParentId() == null;
                }).map(menuEntity -> {
                    //获取其子类菜单
                    menuEntity.setChildrenMenus(getChildrenMenus(menuEntity, menuEntities));
                    return menuEntity;
                }).collect(Collectors.toList());

        return oneLevelMenus;
    }

    @Override
    public MenuEntity getMenuById(Long id) {
        return menuDao.selectOne(new QueryWrapper<MenuEntity>().eq("id", id));
    }

    @Override
    public boolean getChildrenMenuByParentId(Long id) {
        List<MenuEntity> menuEntities = menuDao.selectList(new QueryWrapper<MenuEntity>().eq("parent_id", id));
        if (menuEntities != null && menuEntities.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteMenuById(Long id) {
        try {
            //先删除 资源（菜单）-角色关联表
            roleMenuDao.delete(new QueryWrapper<RoleMenuEntity>().eq("menu_id", id));
            menuDao.deleteById(id);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

    /**
     * 获取某个菜单的子类菜单
     *
     * @param menuEntity   要获取子类菜单的菜单
     * @param menuEntities 所有菜单信息
     * @return
     */
    private List<MenuEntity> getChildrenMenus(MenuEntity menuEntity, List<MenuEntity> menuEntities) {
        return menuEntities.stream()
                .filter(menuEntity1 -> {
                    return menuEntity1.getParentId() != null && menuEntity1.getParentId().equals(menuEntity.getId());
                }).map(menuEntity1 -> {
                    menuEntity1.setChildrenMenus(getChildrenMenus(menuEntity1, menuEntities));
                    return menuEntity1;
                }).collect(Collectors.toList());
    }

}